package com.sevenGroup.pawnshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenGroup.pawnshop.Utils.GetJson;
import com.sevenGroup.pawnshop.Utils.GetObjectByJson;
import com.sevenGroup.pawnshop.controller.form.SecondTypeForm;
import com.sevenGroup.pawnshop.controller.form.SencondTypePageForm;
import com.sevenGroup.pawnshop.controller.info.SecondTypeInfo;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.FirstType;
import com.sevenGroup.pawnshop.model.Goods;
import com.sevenGroup.pawnshop.model.SecondType;
import com.sevenGroup.pawnshop.dao.SecondTypeDao;
import com.sevenGroup.pawnshop.model.Select;
import com.sevenGroup.pawnshop.service.FirstTypeService;
import com.sevenGroup.pawnshop.service.SecondTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@Service
public class SecondTypeServiceImpl extends ServiceImpl<SecondTypeDao, SecondType> implements SecondTypeService {
    @Autowired
    FirstTypeService firstTypeService;
    @Autowired
    GoodsServiceImpl goodsService;
    @Transactional
    @Override
    public void saveAndUpdate(SecondTypeForm secondTypeForm) throws sevenGroupException {
        if (secondTypeForm.getSecondTypeName()==null||secondTypeForm.getSecondTypeName().isEmpty()||secondTypeForm.getFirstTypeId()==null
        ||secondTypeForm.getFirstTypeName()==null||secondTypeForm.getFirstTypeName().isEmpty()) {
            throw  new sevenGroupException(409,"操作失败");
        }
        FirstType byId = firstTypeService.getById(secondTypeForm.getFirstTypeId());
        if (!secondTypeForm.getFirstTypeName().equals(byId.getFirstTypeName())) {
            throw new sevenGroupException(409,"操作失败");
        }
        SecondType secondType = new SecondType();
        secondType.setFirstTypeId(secondTypeForm.getFirstTypeId());
        secondType.setFirstTypeName(secondTypeForm.getFirstTypeName());
        secondType.setSecondTypeName(secondTypeForm.getSecondTypeName());
        if (secondTypeForm.getSecondTypeDescribe()!=null) {
            secondType.setSecondTypeDescribe(secondTypeForm.getSecondTypeDescribe());
        }
        if (secondTypeForm.getSecondTypeId()!=null) {
            secondType.setSecondTypeId(secondTypeForm.getSecondTypeId());
        }
        if (secondTypeForm.getSecondTypeImgurl()!=null&&secondTypeForm.getSecondTypeImgurl().size()>0) {
            String key = GetJson.getListJson("secondTypeImgurl",secondTypeForm.getSecondTypeImgurl());
            secondType.setSecondTypeImgurl(key);
        }
        if (secondTypeForm.getSecondTypeUnit()!=null) {
            secondType.setSecondTypeUnit(secondTypeForm.getSecondTypeUnit());
        }
        if (secondTypeForm.getSecondTypeLdentificationFigure()!=null&&secondTypeForm.getSecondTypeLdentificationFigure().size()>0){
            String listJson = GetJson.getListJson("secondTypeLdentificationFigure",secondTypeForm.getSecondTypeLdentificationFigure());
            secondType.setSecondTypeLdentificationFigure(listJson);
        }
        if (secondTypeForm.getSecondTypeDisplay()!=null) {
            secondType.setSecondTypeDisplay(secondTypeForm.getSecondTypeDisplay());
        }
        secondType.setSecondTypeSort(secondTypeForm.getSecondTypeSort());
        boolean b = saveOrUpdate(secondType);
        if (!b){
            throw new sevenGroupException(408,"添加失败");
        }
           if(secondTypeForm.getSecondTypeId()!=null){
               Goods goods = new Goods();
               goods.setGoodSecondTypeId(secondTypeForm.getSecondTypeId());
               goods.setGoodSecondType(secondTypeForm.getSecondTypeName());
               goodsService.update(goods,new QueryWrapper<Goods>());
           }

    }

    @Override
    public List<SecondTypeInfo> selectAll() throws sevenGroupException {
        List<SecondType> list = list(new QueryWrapper<SecondType>().orderByDesc("second_type_sort"));
        if (list.size()<1) {
                throw new sevenGroupException(409,"目前没有分类");
        }
        List<SecondTypeInfo> secondTypeInfo = getSecondTypeInfo(list);
        return secondTypeInfo;
    }
    @Transactional
    @Override
    public void del(List<Integer> id) throws sevenGroupException {
        if (id==null) {
            throw new sevenGroupException(409,"操作失败");
        }
        List<SecondType> secondTypes = listByIds(id);
        if (secondTypes==null) {
            throw new sevenGroupException(409,"该分类不存在");
        }
        removeByIds(id);
    }


    @Override
    public IPage<SecondTypeInfo> selectByName(SencondTypePageForm sencondTypePageForm) throws sevenGroupException {
        QueryWrapper wrapper = new QueryWrapper<SecondType>();
        if (sencondTypePageForm.getPage()==null) {
                throw new sevenGroupException(409,"分页查询错误");
        }
        if (sencondTypePageForm.getId()!=null) {
            wrapper.eq("first_type_id",sencondTypePageForm.getId());
        }
        if (sencondTypePageForm.getName()!=null) {
            wrapper.like("second_type_name",sencondTypePageForm.getName());
        }
        wrapper.orderByDesc("second_type_sort");
        IPage<SecondType> page = page(sencondTypePageForm.getPage(),wrapper);
        List<SecondType> records = page.getRecords();
        List<SecondTypeInfo> secondTypeInfo = getSecondTypeInfo(records);
        IPage<SecondTypeInfo> info = new Page<SecondTypeInfo>();
        info.setRecords(secondTypeInfo);
        info.setCurrent(page.getCurrent());
        info.setTotal(page.getTotal());
        info.setSize(page.getSize());
        return info;
    }

    @Override
    public List<SecondTypeInfo> selectByFirstId(Integer firstId) {
        if (firstId==null){
            List<SecondType> list = list(new QueryWrapper<SecondType>().eq("second_type_display",0));
            return getSecondTypeInfo(list);
        }else {
            List<SecondType> list = list(new QueryWrapper<SecondType>().eq("first_type_id",firstId).eq("second_type_display",0));
            return getSecondTypeInfo(list);
        }
    }

    //转换专用
    private static List<SecondTypeInfo> getSecondTypeInfo(List<SecondType> list){
        List<SecondTypeInfo> listInfo = new ArrayList<>();
        for (SecondType type : list) {
            List<String> list1=null;
            if (type.getSecondTypeImgurl()!=null) {
                list1 = GetObjectByJson.getList("secondTypeImgurl", type.getSecondTypeImgurl(), String.class);
            }
            List<Select> list2 =null;
            if (type.getSecondTypeLdentificationFigure()!=null) {
                list2 = GetObjectByJson.getList("secondTypeLdentificationFigure", type.getSecondTypeLdentificationFigure(),Select.class);

            }
            SecondTypeInfo info = new SecondTypeInfo();
            info.setSecondTypeImgurl(list1);
            info.setSecondTypeLdentificationFigure(list2);
            info.setFirstTypeName(type.getFirstTypeName());
            info.setFirstTypeId(type.getFirstTypeId());
            info.setSecondTypeDescribe(type.getSecondTypeDescribe());
            info.setSecondTypeName(type.getSecondTypeName());
            info.setSecondTypeId(type.getSecondTypeId());
            info.setSecondTypeDisplay(type.getSecondTypeDisplay());
            info.setSecondTypeUnit(type.getSecondTypeUnit());
            info.setSecondTypeSort(type.getSecondTypeSort());
            listInfo.add(info);
        }
        return listInfo;
    }
}
